<?php
ini_set('memory_limit', '50M');
ini_set('set_time_limit', 1800);
set_include_path($_SERVER['DOCUMENT_ROOT']);
$error = "网页状态已经过期，请刷新页面";
include_once 'admin/includes/functions/admin-functions.php';
global $DBobject, $SMARTY, $CONFIG;
//后台认证
if (empty($_SESSION['user']['admin'])) {
    header("location: http://www.readysteadygokids.com.cn/admin/login");
    die();
}
selectInfo();
//数据查询
$sql = "SELECT * FROM tbl_news_management WHERE deleted IS NULL ORDER BY modified DESC";
$res = $DBobject->executeSQL($sql);
$SMARTY->assign("newList", unclean($res));

if(isset($_GET['newId']) && !empty($_GET['newId'])){
    $SMARTY->assign("editState", 'edit'); //编辑状态
    $SMARTY->assign("newid", $_GET['newId']);//新闻id
    $newId = $_GET['newId'];
    $sql = "SELECT * FROM tbl_news_management WHERE id=$newId ORDER BY modified DESC";
    $res = $DBobject->executeSQL($sql);
    $SMARTY->assign("new", unclean($res)[0]);
}else{
    $SMARTY->assign("editState", 'add'); //添加状态
}

//数据操作
if ($_POST['type'] == 'delete') {
    $sql = "UPDATE tbl_news_management SET deleted = NOW() WHERE id=:id";
    $params = [
        "id" => $_POST['id'],
    ];
    if ($res = $DBobject->wrappedSql($sql,$params)) {
        echo jsonHtml(1, '删除成功');
    } else {
        echo jsonHtml(0, '删除失败');
    }
} elseif($_POST['type'] == 'add') {
    if (checkToken('admin', $_POST['formToken'])) {
        $sql = 'INSERT INTO tbl_news_management (news_title_id, title, content, article, image, publish, start_time, stop_time, created, modified) 
                      VALUES ( :news_title_id, :title, :content, :article, :image, :publish, :start_time, :stop_time, NOW(), NOW() ) ';
        $params = [
            "news_title_id" => $_POST['news_title_id'],
            "title" => $_POST['title'],
            "content" => $_POST['content'],
            "article" => $_POST['article'],
            "image"   => $_POST['image'],
            "publish" => $_POST['publish'],
            "start_time" => $_POST['start_time'],
            "stop_time" => $_POST['stop_time'],
        ];
        if ($_POST['news_title_id'] == '' || $_POST['title'] == ''  || $_POST['content'] == '' || $_POST['article'] == '' || $_POST['start_time'] == '' || $_POST['image'] == '' ) {
            echo jsonHtml(0, '数据不能为空!');
        }
        $res = $DBobject->wrappedSql($sql,$params);
        echo jsonHtml(1, '添加成功');
    } else {
        echo jsonHtml(0, '添加失败');
    }
} elseif($_POST['type'] == 'edit') {
    //表单提交
    if (checkToken('admin', $_POST['formToken'])) {
        $sql = "UPDATE tbl_news_management SET news_title_id = :news_title_id, title = :title, content = :content, article = :article, image = :image, publish = :publish, start_time = :start_time, stop_time = :stop_time, modified = NOW() WHERE id=:id ";
        $params = [
            "news_title_id" => $_POST['news_title_id'],
            "title" => $_POST['title'],
            "content" => $_POST['content'],
            "article" => $_POST['article'],
            "image" => $_POST['image'],
            "publish" => $_POST['publish'],
            "start_time" => $_POST['start_time'],
            "stop_time" => $_POST['stop_time'],
            "id" => $_POST['id'],
        ];
        if ($_POST['id'] == '' || $_POST['news_title_id'] == '' || $_POST['title'] == ''  || $_POST['content'] == '' || $_POST['article'] == '' || $_POST['start_time'] == '' || $_POST['image'] == '' ) {
            echo jsonHtml(0, '数据不能为空!');
        }
        $res = $DBobject->wrappedSql($sql,$params);
        echo jsonHtml(1, '编辑成功');
    } else {
        echo jsonHtml(0, '编辑失败');
    }
} elseif($_POST['type'] == 'edit_publish') {
    //表单提交
    if (checkToken('admin', $_POST['formToken'])) {
        $sql = "UPDATE tbl_news_management SET publish = :publish, start_time = :start_time, stop_time = :stop_time, modified = NOW() WHERE id=:id ";
        $start_time=date("Y-m-d",time());
//        $stop_time=date("Y-m-d",strtotime("+1 month"));
        $params = [
            "publish" => $_POST['publish'],
            "start_time" => $start_time,
            "stop_time" => '',
            "id" => $_POST['id']
        ];
        if ($_POST['publish'] == '' ) {
            echo jsonHtml(0, '操作失败!');
        }
        $res = $DBobject->wrappedSql($sql,$params);
        echo jsonHtml(1, '操作成功');
    } else {
        echo jsonHtml(0, '操作失败');
    }
}

//json
function jsonHtml ($type , $content) {
    return json_encode(array(
        'type' => $type,
        'content' => $content
    ));
    die();
}

//主标题查询
function selectInfo () {
    global $DBobject, $SMARTY;
    $sql = "SELECT * FROM tbl_news_title WHERE deleted IS NULL ORDER BY modified DESC";
    $res = $DBobject->executeSQL($sql);
    $SMARTY->assign("tabs", unclean($res));
}



